// pages/monneyManagement/index.js
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        active: 0,
        show: '',
        searchForm: {},
        pageNum: 1,
        resultList: [],
        ids: [],
        moneyTotal: 0,
        pageSize: 300,
        pageSizeIndex: 3,
        url: '/api/classify/selChangeList',
        allChecked: false,
        deleteUrl: '/api/classify/delChangeId',
        isPages: true,
        permissions: false,
        admin: '0',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options.type)
        if (options.type == 1) {
            const url = '/api/classify/selCollectionList';
            const deleteUrl = '/api/classify/delCollectionId';
            this.setData({
                active: 1, 
                url,
                deleteUrl
            });
        }
        wx.hideShareMenu();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getResultData(this.data.url);
        this.setPermissions();
        this.setData({ admin: wx.getStorageSync('admin') });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        // if(!this.data.isPages) return;
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    resetIsPage() {
        this.setData({ isPages: true });
    },

    resetData() {
        this.setData({ pageNum: 1, pageSize: 300, pageSizeIndex: 3, isPages: true });
        this.childComponent = this.selectComponent("#moneySearch");
        this.childComponent.reset();
    },

    getResultData(url) {
        wx.showLoading();
        wx.request({
            url: `${app.globalData.baseUrl}${url}`,
            method: 'GET',
            header: {
                'token': wx.getStorageSync('token'),
                "Content-Type": "application/json"
            },
            data: {
                pageSize: this.data.pageSize,
                pageNum: this.data.pageNum,
                ...this.data.searchForm,
                financeId: 1,
                isDisable: this.data.searchForm.isDisable === 0 ? 0 : 1,
            },
            success: (res) => {
                if (res.data.code == 0) {
                    let flag = true;
                    if (res.data.rows.length >= res.data.total) {
                        flag = false;
                    }
                    let num = 0;
                    res.data.rows?.forEach((item, index) => {
                        item.isCheckBox = false;
                        if(item.money && !isNaN(item.money)) num += parseFloat(item.money);
                        item.index = res.data.rows?.length - index;
                    });
                    this.setData({
                        resultList: res.data.rows,
                        isPages: flag,
                        allChecked: false,
                        moneyTotal: num
                    });
                    this.onClose();
                    wx.hideLoading();
                } else {
                    wx.showToast({
                        title: '系统错误',
                    });
                    wx.hideLoading();
                }
            },
            fail: () => {
                wx.hideLoading();
                wx.showToast({
                    title: '系统错误',
                });
            }
        })
    },

    setPermissions() {
        wx.request({
            url: `${app.globalData.baseUrl}/api/finance/getUserDept`,
            method: "GET",
            header: {
                'token': wx.getStorageSync('token'),
                "Content-Type": "application/json"
            },
            success: (res) => {
                if (res.data.code == 0) {
                    const deptIds = [];
                    res?.data?.data?.forEach(item => {
                        deptIds.push(item.deptId);
                    });
                    if (deptIds.length === 0 && wx.getStorageSync('admin') != '1') {
                        wx.switchTab({
                            url: '/pages/index/index',
                        });
                        return;
                    }
                    if (deptIds.indexOf(3) > -1 && deptIds.length > 1) {
                        this.setData({
                            permissions: true
                        });
                    }
                }
            }
        })
    },

    onChange(event) {
        let url = '/api/classify/selChangeList';
        let deleteUrl = '/api/classify/delChangeId';
        if (event.detail.name == 1) {
            url = '/api/classify/selCollectionList';
            deleteUrl = '/api/classify/delCollectionId';
        }
        this.setData({
            url,
            pageNum: 1,
            pageSize: 300,
            pageSizeIndex: 3,
            deleteUrl,
            isPages: true,
        });
        this.childComponent = this.selectComponent("#moneySearch");
        this.childComponent.reset();
    },

    searchShow() {
        this.setData({
            show: true
        });
    },

    onClose() {
        this.setData({
            show: false
        });
    },

    search(e) {
        this.setData({
            searchForm: { ...e.detail },
            pageNum: 1,
        });
        this.getResultData(this.data.url);
    },

    onSelectPageSize(e) {
        this.setData({
            pageSizeIndex: e.detail.index,
            pageSize: e.detail.pageSize,
            pageNum: 1,
        });
        this.getResultData(this.data.url);
    },

    onAllChange(e) {
        this.setData({
            allChecked: e.detail.allChecked,
            resultList: e.detail.list,
            ids: e.detail.ids,
        });
    },

    deleteGetData(e) {
        this.deleteItem(e.detail);
    },

    deleteItem(ids) {
        const _that = this;
        wx.showModal({
            content: '确认删除吗？',
            success: function (sm) {
                if (sm.confirm) {
                    wx.showLoading();
                    wx.request({
                        url: `${app.globalData.baseUrl}${_that.data.deleteUrl}?id=${ids}`,
                        method: "GET",
                        header: {
                            'token': wx.getStorageSync('token'),
                            "Content-Type": "application/json"
                        },
                        success: (res) => {
                            wx.hideLoading();
                            if (res.data.code === 0) {
                                _that.getResultData(_that.data.url);
                            }
                        },
                        fail: () => {
                            wx.hideLoading();
                            wx.showToast({
                                title: '系统错误',
                            });
                        }
                    })
                }
            }
        });
    },

    moreDelete() {
        if (this.data.ids.length == 0) {
            wx.showModal({
                content: '请在列表选择需要删除的数据！',
                showCancel: false,
            })
        } else {
            this.deleteItem(this.data.ids?.join(','));
        }
    },

    scrollBottm(e) {
        if (!this.data.isPages) return;
        var pages = this.data.pageNum++;
        var page = pages + 1;
        wx.showLoading();
        wx.request({
            url: `${app.globalData.baseUrl}${this.data.url}`,
            method: 'GET',
            header: {
                'token': wx.getStorageSync('token'),
                "Content-Type": "application/json"
            },
            data: {
                pageSize: this.data.pageSize,
                pageNum: page,
                ...this.data.searchForm,
                isDisable: this.data.searchForm.isDisable === 0 ? 0 : 1,
            },
            success: (res) => {
                if (res.data.code == 0) {
                    let num = 0;
                    var resultList = this.data.resultList.concat(res.data.rows);
                    resultList?.forEach((item, index) => {
                        item.isCheckBox = false;
                        num += parseFloat(item.money);
                    });
                    let flag = true;
                    if (resultList.length >= res.data.total) {
                        flag = false;
                    }
                    this.setData({
                        resultList: resultList,
                        moneyTotal: num,
                        pageNum: page,
                        isPages: flag
                    });
                    this.onClose();
                    wx.hideLoading();
                } else {
                    wx.hideLoading();
                    wx.showToast({
                        title: '系统错误',
                    });
                }
            },
            fail: () => {
                wx.hideLoading();
                wx.showToast({
                    title: '系统错误',
                });
            }
        })
    }
})